package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notices")
@Tag(name = "公告管理",description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")
public class NoticesController {
    @Autowired
    private NoticesService noticesService;

    @Operation(summary = "更新公告信息", description = "修改公告的内容、标题、类型等信息")
    @PutMapping("/update")
    public Result updateNotices(@Parameter(name = "notice", description = "系统公告信息")
                                @RequestBody Notice notice) {
        Boolean r = noticesService.updateById(notice);
        return r ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Operation(summary = "切换公告状态", description = "启用或禁用指定的公告，禁用后不会在前台显示")
    @PutMapping("/toggle/{id}")
    public Result toggleNotice(@Parameter(name = "id", description = "公告ID") @PathVariable Long id,
                               @Parameter(name = "isActive", description = "是否启用，true为启用，false为禁用")
                               @RequestParam Boolean isActive) {
        LambdaUpdateWrapper<Notice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Notice::getIsActive, isActive).eq(Notice::getId, id);
        boolean r = noticesService.update(updateWrapper);
        return r ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Operation(summary = "发布新公告", description = "创建并发布新的系统公告")
    @PostMapping("/add")
    public Result addNotice(@Parameter(name = "notice", description = "系统公告信息") @RequestBody Notice notice) {
        boolean r = noticesService.save(notice);
        return r ? Result.success("添加成功") : Result.error("添加失败");
    }
    @Operation(summary = "根据ID获取公告",description = "根据公告ID获取单个公告的详细信息")
    @GetMapping("/{id}")
    public Result<Notice> getNoticeById(@Parameter(name = "id",description = "公告ID") @PathVariable Long id) {
        Notice notice = noticesService.getById(id);
        return notice ==null ? Result.error("添加失败") : Result.success(notice);
    }

    @Operation(summary = "获取所有公告", description = "获取所有公告列表，包括启用和禁用的，供管理后台使用")
    @GetMapping("/list")
    public Result<List<Notice>> getAllNotice() {
        List<Notice> list = noticesService.list();
        return list.isEmpty() ? Result.error("系统故障") : Result.success(list);
    }

    @Operation(summary = "获取最新公告", description = "获取最新发布的公告列表，用于首页推荐展示")
    @GetMapping("/latest")
    public Result<List<Notice>> getLatestNotice(@Parameter(name = "limit", description = "限制数量")
                                                @RequestParam String limit) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<Notice>();
        queryWrapper.orderByDesc(Notice::getCreateTime).last("limit " + limit);
        List<Notice> list = noticesService.list(queryWrapper);

        return !list.isEmpty() ? Result.success(list) : Result.error("系统故障");
    }

    @Operation(summary = "获取启用的公告", description = "获取状态为启用的公告列表，供前台首页展示使用")
    @GetMapping("/active")
    public Result<List<Notice>> getActiveNotice() {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getIsActive, true);
        List<Notice> list = noticesService.list(queryWrapper);
        return !list.isEmpty() ? Result.success(list) : Result.error("系统故障");
    }

    @Operation(summary = "删除公告", description = "根据ID删除指定的公告")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@Parameter(name = "id", description = "公告ID") @PathVariable Long id) {
        boolean r = noticesService.removeById(id);
        return r ? Result.success("删除成功") : Result.error("删除失败");
    }


}
